<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>28.11. gc — Garbage Collector interface &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="28. Python Runtime Services" href="python.html" />
    <link rel="next" title="28.12. inspect — Inspect live objects" href="inspect.html" />
    <link rel="prev" title="28.10. __future__ — Future statement definitions" href="__future__.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="inspect.html" title="28.12. inspect — Inspect live objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="__future__.html" title="28.10. __future__ — Future statement definitions"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="python.html" accesskey="U">28. Python Runtime Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-gc">
<h1>28.11. <tt class="xref docutils literal"><span class="pre">gc</span></tt> &#8212; Garbage Collector interface<a class="headerlink" href="#module-gc" title="Permalink to this headline">¶</a></h1>
<p>This module provides an interface to the optional garbage collector.  It
provides the ability to disable the collector, tune the collection frequency,
and set debugging options.  It also provides access to unreachable objects that
the collector found but cannot free.  Since the collector supplements the
reference counting already used in Python, you can disable the collector if you
are sure your program does not create reference cycles.  Automatic collection
can be disabled by calling <tt class="docutils literal"><span class="pre">gc.disable()</span></tt>.  To debug a leaking program call
<tt class="docutils literal"><span class="pre">gc.set_debug(gc.DEBUG_LEAK)</span></tt>. Notice that this includes
<tt class="docutils literal"><span class="pre">gc.DEBUG_SAVEALL</span></tt>, causing garbage-collected objects to be saved in
gc.garbage for inspection.</p>
<p>The <tt class="xref docutils literal"><span class="pre">gc</span></tt> module provides the following functions:</p>
<dl class="function">
<dt id="gc.enable">
<tt class="descclassname">gc.</tt><tt class="descname">enable</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.enable" title="Permalink to this definition">¶</a></dt>
<dd>Enable automatic garbage collection.</dd></dl>

<dl class="function">
<dt id="gc.disable">
<tt class="descclassname">gc.</tt><tt class="descname">disable</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.disable" title="Permalink to this definition">¶</a></dt>
<dd>Disable automatic garbage collection.</dd></dl>

<dl class="function">
<dt id="gc.isenabled">
<tt class="descclassname">gc.</tt><tt class="descname">isenabled</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.isenabled" title="Permalink to this definition">¶</a></dt>
<dd>Returns true if automatic collection is enabled.</dd></dl>

<dl class="function">
<dt id="gc.collect">
<tt class="descclassname">gc.</tt><tt class="descname">collect</tt><big>(</big><span class="optional">[</span><em>generation</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#gc.collect" title="Permalink to this definition">¶</a></dt>
<dd><p>With no arguments, run a full collection.  The optional argument <em>generation</em>
may be an integer specifying which generation to collect (from 0 to 2).  A
<a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is raised if the generation number  is invalid. The number of
unreachable objects found is returned.</p>
<p>
<span class="versionmodified">Changed in version 2.5: </span>The optional <em>generation</em> argument was added.</p>
<p>
<span class="versionmodified">Changed in version 2.6: </span>The free lists maintained for a number of builtin types are cleared
whenever a full collection or collection of the highest generation (2)
is run.  Not all items in some free lists may be freed due to the
particular implementation, in particular <a title="int" class="reference external" href="functions.html#int"><tt class="xref docutils literal"><span class="pre">int</span></tt></a> and <a title="float" class="reference external" href="functions.html#float"><tt class="xref docutils literal"><span class="pre">float</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="gc.set_debug">
<tt class="descclassname">gc.</tt><tt class="descname">set_debug</tt><big>(</big><em>flags</em><big>)</big><a class="headerlink" href="#gc.set_debug" title="Permalink to this definition">¶</a></dt>
<dd>Set the garbage collection debugging flags. Debugging information will be
written to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>.  See below for a list of debugging flags which can be
combined using bit operations to control debugging.</dd></dl>

<dl class="function">
<dt id="gc.get_debug">
<tt class="descclassname">gc.</tt><tt class="descname">get_debug</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_debug" title="Permalink to this definition">¶</a></dt>
<dd>Return the debugging flags currently set.</dd></dl>

<dl class="function">
<dt id="gc.get_objects">
<tt class="descclassname">gc.</tt><tt class="descname">get_objects</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_objects" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a list of all objects tracked by the collector, excluding the list
returned.</p>
<p>
<span class="versionmodified">New in version 2.2.</span></p>
</dd></dl>

<dl class="function">
<dt id="gc.set_threshold">
<tt class="descclassname">gc.</tt><tt class="descname">set_threshold</tt><big>(</big><em>threshold0</em><span class="optional">[</span>, <em>threshold1</em><span class="optional">[</span>, <em>threshold2</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#gc.set_threshold" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the garbage collection thresholds (the collection frequency). Setting
<em>threshold0</em> to zero disables collection.</p>
<p>The GC classifies objects into three generations depending on how many
collection sweeps they have survived.  New objects are placed in the youngest
generation (generation <tt class="docutils literal"><span class="pre">0</span></tt>).  If an object survives a collection it is moved
into the next older generation.  Since generation <tt class="docutils literal"><span class="pre">2</span></tt> is the oldest
generation, objects in that generation remain there after a collection.  In
order to decide when to run, the collector keeps track of the number object
allocations and deallocations since the last collection.  When the number of
allocations minus the number of deallocations exceeds <em>threshold0</em>, collection
starts.  Initially only generation <tt class="docutils literal"><span class="pre">0</span></tt> is examined.  If generation <tt class="docutils literal"><span class="pre">0</span></tt> has
been examined more than <em>threshold1</em> times since generation <tt class="docutils literal"><span class="pre">1</span></tt> has been
examined, then generation <tt class="docutils literal"><span class="pre">1</span></tt> is examined as well.  Similarly, <em>threshold2</em>
controls the number of collections of generation <tt class="docutils literal"><span class="pre">1</span></tt> before collecting
generation <tt class="docutils literal"><span class="pre">2</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="gc.get_count">
<tt class="descclassname">gc.</tt><tt class="descname">get_count</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current collection  counts as a tuple of <tt class="docutils literal"><span class="pre">(count0,</span> <span class="pre">count1,</span>
<span class="pre">count2)</span></tt>.</p>
<p>
<span class="versionmodified">New in version 2.5.</span></p>
</dd></dl>

<dl class="function">
<dt id="gc.get_threshold">
<tt class="descclassname">gc.</tt><tt class="descname">get_threshold</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_threshold" title="Permalink to this definition">¶</a></dt>
<dd>Return the current collection thresholds as a tuple of <tt class="docutils literal"><span class="pre">(threshold0,</span>
<span class="pre">threshold1,</span> <span class="pre">threshold2)</span></tt>.</dd></dl>

<dl class="function">
<dt id="gc.get_referrers">
<tt class="descclassname">gc.</tt><tt class="descname">get_referrers</tt><big>(</big><em>*objs</em><big>)</big><a class="headerlink" href="#gc.get_referrers" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the list of objects that directly refer to any of objs. This function
will only locate those containers which support garbage collection; extension
types which do refer to other objects but do not support garbage collection will
not be found.</p>
<p>Note that objects which have already been dereferenced, but which live in cycles
and have not yet been collected by the garbage collector can be listed among the
resulting referrers.  To get only currently live objects, call <a title="gc.collect" class="reference internal" href="#gc.collect"><tt class="xref docutils literal"><span class="pre">collect()</span></tt></a>
before calling <a title="gc.get_referrers" class="reference internal" href="#gc.get_referrers"><tt class="xref docutils literal"><span class="pre">get_referrers()</span></tt></a>.</p>
<p>Care must be taken when using objects returned by <a title="gc.get_referrers" class="reference internal" href="#gc.get_referrers"><tt class="xref docutils literal"><span class="pre">get_referrers()</span></tt></a> because
some of them could still be under construction and hence in a temporarily
invalid state. Avoid using <a title="gc.get_referrers" class="reference internal" href="#gc.get_referrers"><tt class="xref docutils literal"><span class="pre">get_referrers()</span></tt></a> for any purpose other than
debugging.</p>
<p>
<span class="versionmodified">New in version 2.2.</span></p>
</dd></dl>

<dl class="function">
<dt id="gc.get_referents">
<tt class="descclassname">gc.</tt><tt class="descname">get_referents</tt><big>(</big><em>*objs</em><big>)</big><a class="headerlink" href="#gc.get_referents" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of objects directly referred to by any of the arguments. The
referents returned are those objects visited by the arguments&#8217; C-level
<a title="tp_traverse" class="reference external" href="../c-api/typeobj.html#tp_traverse"><tt class="xref docutils literal"><span class="pre">tp_traverse</span></tt></a> methods (if any), and may not be all objects actually
directly reachable.  <a title="tp_traverse" class="reference external" href="../c-api/typeobj.html#tp_traverse"><tt class="xref docutils literal"><span class="pre">tp_traverse</span></tt></a> methods are supported only by objects
that support garbage collection, and are only required to visit objects that may
be involved in a cycle.  So, for example, if an integer is directly reachable
from an argument, that integer object may or may not appear in the result list.</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<p>The following variable is provided for read-only access (you can mutate its
value but should not rebind it):</p>
<dl class="data">
<dt id="gc.garbage">
<tt class="descclassname">gc.</tt><tt class="descname">garbage</tt><a class="headerlink" href="#gc.garbage" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of objects which the collector found to be unreachable but could not be
freed (uncollectable objects).  By default, this list contains only objects with
<a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a> methods. <a class="footnote-reference" href="#id2" id="id1">[1]</a> Objects that have <a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a> methods and are
part of a reference cycle cause the entire reference cycle to be uncollectable,
including objects not necessarily in the cycle but reachable only from it.
Python doesn&#8217;t collect such cycles automatically because, in general, it isn&#8217;t
possible for Python to guess a safe order in which to run the <a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a>
methods.  If you know a safe order, you can force the issue by examining the
<em>garbage</em> list, and explicitly breaking cycles due to your objects within the
list.  Note that these objects are kept alive even so by virtue of being in the
<em>garbage</em> list, so they should be removed from <em>garbage</em> too.  For example,
after breaking cycles, do <tt class="docutils literal"><span class="pre">del</span> <span class="pre">gc.garbage[:]</span></tt> to empty the list.  It&#8217;s
generally better to avoid the issue by not creating cycles containing objects
with <a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a> methods, and <em>garbage</em> can be examined in that case to
verify that no such cycles are being created.</p>
<p>If <a title="gc.DEBUG_SAVEALL" class="reference internal" href="#gc.DEBUG_SAVEALL"><tt class="xref docutils literal"><span class="pre">DEBUG_SAVEALL</span></tt></a> is set, then all unreachable objects will be added to
this list rather than freed.</p>
</dd></dl>

<p>The following constants are provided for use with <a title="gc.set_debug" class="reference internal" href="#gc.set_debug"><tt class="xref docutils literal"><span class="pre">set_debug()</span></tt></a>:</p>
<dl class="data">
<dt id="gc.DEBUG_STATS">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_STATS</tt><a class="headerlink" href="#gc.DEBUG_STATS" title="Permalink to this definition">¶</a></dt>
<dd>Print statistics during collection.  This information can be useful when tuning
the collection frequency.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_COLLECTABLE">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_COLLECTABLE</tt><a class="headerlink" href="#gc.DEBUG_COLLECTABLE" title="Permalink to this definition">¶</a></dt>
<dd>Print information on collectable objects found.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_UNCOLLECTABLE">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_UNCOLLECTABLE</tt><a class="headerlink" href="#gc.DEBUG_UNCOLLECTABLE" title="Permalink to this definition">¶</a></dt>
<dd>Print information of uncollectable objects found (objects which are not
reachable but cannot be freed by the collector).  These objects will be added to
the <tt class="docutils literal"><span class="pre">garbage</span></tt> list.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_INSTANCES">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_INSTANCES</tt><a class="headerlink" href="#gc.DEBUG_INSTANCES" title="Permalink to this definition">¶</a></dt>
<dd>When <a title="gc.DEBUG_COLLECTABLE" class="reference internal" href="#gc.DEBUG_COLLECTABLE"><tt class="xref docutils literal"><span class="pre">DEBUG_COLLECTABLE</span></tt></a> or <a title="gc.DEBUG_UNCOLLECTABLE" class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE"><tt class="xref docutils literal"><span class="pre">DEBUG_UNCOLLECTABLE</span></tt></a> is set, print
information about instance objects found.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_OBJECTS">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_OBJECTS</tt><a class="headerlink" href="#gc.DEBUG_OBJECTS" title="Permalink to this definition">¶</a></dt>
<dd>When <a title="gc.DEBUG_COLLECTABLE" class="reference internal" href="#gc.DEBUG_COLLECTABLE"><tt class="xref docutils literal"><span class="pre">DEBUG_COLLECTABLE</span></tt></a> or <a title="gc.DEBUG_UNCOLLECTABLE" class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE"><tt class="xref docutils literal"><span class="pre">DEBUG_UNCOLLECTABLE</span></tt></a> is set, print
information about objects other than instance objects found.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_SAVEALL">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_SAVEALL</tt><a class="headerlink" href="#gc.DEBUG_SAVEALL" title="Permalink to this definition">¶</a></dt>
<dd>When set, all unreachable objects found will be appended to <em>garbage</em> rather
than being freed.  This can be useful for debugging a leaking program.</dd></dl>

<dl class="data">
<dt id="gc.DEBUG_LEAK">
<tt class="descclassname">gc.</tt><tt class="descname">DEBUG_LEAK</tt><a class="headerlink" href="#gc.DEBUG_LEAK" title="Permalink to this definition">¶</a></dt>
<dd>The debugging flags necessary for the collector to print information about a
leaking program (equal to <tt class="docutils literal"><span class="pre">DEBUG_COLLECTABLE</span> <span class="pre">|</span> <span class="pre">DEBUG_UNCOLLECTABLE</span> <span class="pre">|</span>
<span class="pre">DEBUG_INSTANCES</span> <span class="pre">|</span> <span class="pre">DEBUG_OBJECTS</span> <span class="pre">|</span> <span class="pre">DEBUG_SAVEALL</span></tt>).</dd></dl>

<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Prior to Python 2.2, the list contained all instance objects in unreachable
cycles,  not only those with <a title="object.__del__" class="reference external" href="../reference/datamodel.html#object.__del__"><tt class="xref docutils literal"><span class="pre">__del__()</span></tt></a> methods.</td></tr>
</tbody>
</table>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="__future__.html"
                                  title="previous chapter">28.10. <tt class="docutils literal docutils literal"><span class="pre">__future__</span></tt> &#8212; Future statement definitions</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="inspect.html"
                                  title="next chapter">28.12. <tt class="docutils literal"><span class="pre">inspect</span></tt> &#8212; Inspect live objects</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/gc.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="inspect.html" title="28.12. inspect — Inspect live objects"
             >next</a> |</li>
        <li class="right" >
          <a href="__future__.html" title="28.10. __future__ — Future statement definitions"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="python.html" >28. Python Runtime Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>